/**
 * 电子照片策略
 */

<template>
  <div class="courseBox">
    <div class="courseBoxTop">
      <div class="courseBoxTopSearch">
        <div class="courseName">
          <span>策略名称：</span>
          <el-input v-model="strategyName" placeholder="请输入策略名称" />
        </div>
        <div class="pictureVersion">
          <span>时间阶段：</span>
          <div class="block">
            <el-date-picker
              v-model="beginTime"
              align="right"
              type="date"
              placeholder="开始时间"
            >
            </el-date-picker>
            <span>-</span>
            <el-date-picker
              v-model="endTime"
              align="right"
              type="date"
              placeholder="结束时间"
            >
            </el-date-picker>
          </div>
        </div>

        <div class="searchAndReset">
          <el-button class="searchBtn" @click="searchInput">
            <i class="el-icon-search"></i>
            <span>搜索</span>
          </el-button>
          <el-button class="resetBtn" @click="refreshInput">
            <i class="el-icon-refresh"></i>
            <span>重置</span>
          </el-button>
        </div>
      </div>
    </div>

    <div class="courseBoxBottom">
      <div class="bottomHeader">
        <span class="title">管理列表</span>
        <div class="bottomHeaderBtn">
          <el-button class="addBtn" @click="openAddPicture">
            <i class="el-icon-plus"></i>
            <span>新增</span>
          </el-button>
          <el-button class="delBtn" @click="batchDelPicture">
            <i class="el-icon-close"></i>
            <span>批量删除</span>
          </el-button>
          <el-button class="toLeadBtn" @click.stop="setIsMaskOpen">
            <span class="icon iconfont icon-daoru"></span>
            <span>批量导入</span>

            <!-- 弹框 -->
            <div
              class="toLeadBtnMask"
              v-if="isMask"
              @mouseenter="setIsMaskOpen"
              @mouseleave="setIsMaskClose"
            >
              <span class="sanjiao"></span>
              <div class="ul">
                <el-upload
                  class="upload-demo"
                  action="https://jsonplaceholder.typicode.com/posts/"
                  :on-preview="handlePreview"
                >
                  <div class="li">
                    <i></i>
                    <el-button size="small" type="primary" class="importCourse">
                      导入电子照片
                    </el-button>
                  </div>
                </el-upload>

                <div class="li">
                  <i></i>
                  <span>下载导入模板</span>
                </div>
              </div>
            </div>
          </el-button>

          <el-button class="exportBtn" @click="exportPicture">
            <span class="icon iconfont icon-daochu"></span>
            <span>导出数据</span>
          </el-button>
        </div>
      </div>
      <div class="bottomMian">
        <!-- 表格 -->
        <el-table
          ref="multipleTable"
          :data="pictureList"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="50"> </el-table-column>
          <el-table-column
            type="index"
            label="序号"
            width="50"
          ></el-table-column>
          <el-table-column
            prop="strategyName"
            label="策略名称"
          ></el-table-column>
          <el-table-column prop="inches" label="图片尺寸"></el-table-column>
          <el-table-column
            prop="photoSize"
            label="图片分辨率"
          ></el-table-column>
          <el-table-column
            prop="applicationType"
            label="应用类型"
          ></el-table-column>
          <el-table-column
            prop="backgroundColor"
            label="图片底色"
          ></el-table-column>
          <el-table-column prop="createTime" label="创建时间" min-width="180">
          </el-table-column>
          <el-table-column label="启用状态">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.photoStatus"
                active-color="#13ce66"
                inactive-color="#C2C2C2"
                @change="updateIsUsing(scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="action" label="操作">
            <template slot-scope="scope">
              <span
                class="icon"
                @click="openEditPicture(scope.row)"
              >编辑</span>
              <span
                class="icon"
                @click="delPicture(scope.row.id)"
              >删除</span>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination" v-if="pictureList.length > 0">
          <el-pagination
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-size="10"
            prev-text="上一页"
            next-text="下一页"
            layout=" prev, pager, next"
            :total="total"
          >
          </el-pagination>
          <div class="jumper">
            跳到第
            <input type="text" ref="currentPage" value="1" />
            页
          </div>
          <button class="submit" @click="goPage">确定</button>
        </div>
      </div>
    </div>

    <!-- 添加 / 编辑电子照片策略 -->
    <div class="dialog">
      <el-dialog
        :title="isEdit ? '编辑电子照片策略' : '添加电子照片策略'"
        :visible.sync="dialogFormVisible"
        center
      >
        <el-form :model="form">
          <!-- <el-form-item label="创建时间:" :label-width="formLabelWidth">
            <div class="block">
              <el-date-picker
                v-model="form.createTime"
                align="center"
                type="date"
                placeholder="请选择时间"
                @focus="selectDate"
                @blur="closeSelect"
              >
              </el-date-picker>
              <i :class="date ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"></i>
            </div>
          </el-form-item> -->
          <el-form-item label="策略名称:" :label-width="formLabelWidth">
            <el-input
              v-model="form.strategyName"
              autocomplete="off"
              placeholder="请输入策略名称"
              style="width: 280px"
            ></el-input>
          </el-form-item>
          <el-form-item
            label="图片分辨率:"
            :label-width="formLabelWidth"
            class="mb"
          >
            <el-input
              v-model="form.photoSize"
              autocomplete="off"
              placeholder="请输入图片分辨率"
              style="width: 240px"
            ></el-input>
            <span>MB</span>
          </el-form-item>
          <el-form-item
            label="图片尺寸:"
            :label-width="formLabelWidth"
            class="px"
          >
            <el-input
              v-model="form.inches"
              autocomplete="off"
              placeholder="请输入图片尺寸"
              style="width: 240px"
            ></el-input>
            <span>寸</span>
          </el-form-item>
          <el-form-item
            label="图片底色:"
            :label-width="formLabelWidth"
            class="px"
          >
            <!-- <el-input
              v-model="form.backgroundColor"
              autocomplete="off"
              placeholder="请选择图片底色"
              style="width: 240px"
            ></el-input>
            <span></span> -->
            <el-select v-model="form.backgroundColor" placeholder="请选择图片底色">
              <el-option 
                v-for="item in backgroundColorOptions"
                :key="item.typeId"
                :label="item.typeName" :value="item.typeName"></el-option>
            </el-select>
          </el-form-item>
           <el-form-item
            label="应用类型:"
            :label-width="formLabelWidth"
            class="px"
          >
            <!-- <el-input
              v-model="form.photoPixelsWidth"
              autocomplete="off"
              placeholder="请选择应用类型"
              style="width: 240px"
            ></el-input> -->
            <el-select v-model="form.applicationType" placeholder="请选择应用类型">
              <el-option 
                v-for="item in applicationTypeOptions"
                :key="item.typeId"
                :label="item.typeName" :value="item.typeName"></el-option>
            </el-select>
          </el-form-item>

          <!-- <el-form-item
            label="图片高度:"
            :label-width="formLabelWidth"
            class="px"
          >
            <el-input
              v-model="form.photoPixelsHeight"
              autocomplete="off"
              placeholder="请输入图片高度"
              style="width: 240px"
            ></el-input>
            <span>PX</span>
          </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeAddPicture">取 消</el-button>
          <el-button type="primary" @click="confirmAddPicture">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import "@/styles/picture.scss";
import { _debounce } from "@/utils/debounce";
import moment from "moment";
import {
  exportApi,
  getPictureListApi,
  addPictureListApi,
  editPictureListApi,
  delPictureListApi,
} from "@/api/basics/picture";

import {getClassifyListApi} from "@/api/basics/classify"

export default {
  data() {
    return {
      pictureList: [],
      majorList: [],
      total: 0,
      isEdit: false,
      date: false,
      strategyName: "",
      multipleSelection: [],
      currentPage: 1,
      pageSize: 10,
      isMask: false,
      beginTime: "",
      endTime: "",
      dialogFormVisible: false,
      form: {
        id: "",
        strategyName: "",
        // createTime: "",
        inches: "",
        backgroundColor: "",
        photoSize: "",
        applicationType:""
      },
      formLabelWidth: "100px",
      filename: "电子照片策略表",
      params: {
        beginTime: "",
        endTime: "",
      },
      backgroundColorOptions:[],
      applicationTypeOptions:[]
    };
  },

  mounted() {
    // 获取电子照片列表
    this.getPictureList({
      pageNum: this.currentPage,
      pageSize: this.pageSize,
      params: this.params,
    });
    getClassifyListApi({parentId:235}).then(res=>{
      this.backgroundColorOptions = res.rows
    })
    getClassifyListApi({parentId:239}).then(res=>{
      this.applicationTypeOptions = res.rows
    })
  },

  methods: {
    // 获取电子照片列表
    getPictureList(data) {
      getPictureListApi(data).then((result) => {
        if (result.code === 200) {
          this.pictureList = result.rows.map((item) => {
            return {
              ...item,
              photoStatus: item.photoStatus == "0" ? true : false,
            };
          });
          this.total = result.total;
        }
      });
    },

    // 搜索
    searchInput: _debounce(function () {
      let data = {
        strategyName: this.strategyName,
        params: {
          beginTime: this.beginTime
            ? moment(this.beginTime).format("YYYY-MM-DD")
            : "",
          endTime: this.endTime
            ? moment(this.endTime).format("YYYY-MM-DD")
            : "",
        },
      };

      this.getPictureList({
        ...data,
        pageNum: this.currentPage,
        pageSize: this.pageSize,
      });
    }, 200),

    // 重置
    refreshInput: _debounce(function () {
      this.strategyName = "";
      this.beginTime = "";
      this.endTime = "";
      this.getPictureList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        params: this.params,
      });
    }, 200),

    // 新增电子照片
    openAddPicture() {
      this.form = {
        id: "",
        strategyName: "",
        // createTime: "",
        photoPixelsWidth: "",
        photoPixelsHeight: "",
        photoSize: "",
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },

    // 编辑电子照片
    openEditPicture(data) {
      console.log(data);
      this.form = data
      // let { id, strategyName, photoSize } = data;
      // this.form = {
      //   id,
      //   strategyName,
      //   // photoPixelsWidth: photoPixels.split("*")[0],
      //   // photoPixelsHeight: photoPixels.split("*")[1],
      //   photoSize,
      // };
      this.isEdit = true;
      this.dialogFormVisible = true;
    },

    // 取消
    closeAddPicture() {
      this.dialogFormVisible = false;
    },

    // 确定
    confirmAddPicture: _debounce(function () {
      // console.log();
      let { strategyName, photoSize, inches, backgroundColor, applicationType } =
        this.form;

      if (strategyName && backgroundColor && inches && photoSize && applicationType) {
        this.dialogFormVisible = false;
        if (this.isEdit) {
          editPictureListApi({
            ...this.form,
            // photoPixels: photoPixelsWidth + "*" + photoPixelsHeight,
          }).then((res) => {
            if (res.code === 200) {
              this.$message.success("修改成功！");
              this.getPictureList({
                pageNum: this.currentPage,
                pageSize: this.pageSize,
                params: this.params,
              });
            } else {
              this.$message.error("修改失败！");
            }
          });
        } else {
          // 添加
          addPictureListApi({
            ...this.form,
            // photoPixels: photoPixelsWidth + "*" + photoPixelsHeight,
            photoStatus: "0",
          }).then((res) => {
            if (res.code === 200) {
              this.$message.success("添加成功！");
              this.getPictureList({
                pageNum: this.currentPage,
                pageSize: this.pageSize,
                params: this.params,
              });
            } else {
              this.$message.error("添加失败！");
            }
          });
        }
      } else if (!strategyName) {
        this.$message.error("策略名称不能为空！");
      } else if (!photoSize) {
        this.$message.error("请输入图片分辨率！");
      } else if (!applicationType) {
        this.$message.error("请选择应用类型！");
      } else if (!backgroundColor) {
        this.$message.error("请选择图片底色！");
      }
    }, 200),

    // 删除
    delPicture(ids) {
      this.$confirm("此操作将删除该电子照片, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delPictureListApi({ ids }).then((res) => {
          if (res.code === 200) {
            this.$message.success("删除成功！");
            this.getPictureList({
              pageNum: this.currentPage,
              pageSize: this.pageSize,
              params: this.params,
            });
          } else {
            this.$message.error("删除失败！");
          }
        });
      });
    },

    // 批量删除
    batchDelPicture() {
      if (this.multipleSelection.length) {
        let data = [...this.multipleSelection].map((item) => {
          return "" + item.id;
        });

        this.$confirm("此操作将删除已选中的电子照片, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          delPictureListApi({ ids: data.join(",") }).then((res) => {
            if (res.code === 200) {
              this.$message.success("删除成功！");
              this.getPictureList({
                pageNum: this.currentPage,
                pageSize: this.pageSize,
                params: this.params,
              });
            } else {
              this.$message.error("删除失败！");
            }
          });
        });
      } else {
        this.$message.error("请选择要删除的电子照片！");
      }
    },

    // 修改启用状态
    updateIsUsing(data) {
      let { id, photoStatus } = data;
      editPictureListApi({
        id,
        photoStatus: photoStatus ? "0" : "1",
      }).then((res) => {
        if (res.code == 200) {
          this.$message.success("修改成功！");
          this.dialogFormVisible = false;
          this.getPictureList({
            pageNum: this.currentPage,
            pageSize: this.pageSize,
            params: this.params,
          });
        } else {
          this.$message.error("修改失败！");
        }
      });
    },

    // 打开导入弹框
    setIsMaskOpen() {
      this.isMask = true;
    },

    // 关闭导入弹框
    setIsMaskClose() {
      this.isMask = false;
    },

    // 导入上传
    handlePreview(file) {
      console.log(file);
    },

    // 导出数据
    exportPicture() {
      this.$confirm("是否导出全部数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        exportApi({
          params: {
            beginTime: "",
            endTime: "",
          },
        }).then((res) => {
          if (!res) return;
          const blob = new Blob([res], {
            type: "application/vnd.ms-excel",
          }); // 构造一个blob对象来处理数据，并设置文件类型
          if (window.navigator.msSaveOrOpenBlob) {
            //兼容IE10
            navigator.msSaveBlob(blob, this.filename);
          } else {
            const href = URL.createObjectURL(blob); //创建新的URL表示指定的blob对象
            const a = document.createElement("a"); //创建a标签
            a.style.display = "none";
            a.href = href; // 指定下载链接
            a.download = this.filename; //指定下载文件名
            a.click(); //触发下载
            URL.revokeObjectURL(a.href); //释放URL对象
          }
        });
      });
    },

    // 选择时间
    selectDate() {
      this.date = true;
    },

    // 取消选择时间
    closeSelect() {
      this.date = false;
    },

    // 已选中
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection);
    },

    // 设置当前页码
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getPictureList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        params: this.params,
      });
    },

    // 跳转指定页码
    goPage() {
      let { value } = this.$refs.currentPage;
      this.currentPage = Number(value);
      this.getPictureList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        params: this.params,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.courseBoxTopSearch {
  ::v-deep .el-input__inner {
    height: 36px;
    line-height: 36px;
  }
}

::v-deep .el-icon-date {
  line-height: 36px;
}

.dialog {
  .block {
    width: 280px;
    height: 30px;
    border: solid 1px #dcdfe6;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-sizing: border-box;
    background-color: #fff;

    ::v-deep .el-input__inner {
      width: 240px;
      height: 28px;
      border: none;
    }

    ::v-deep .el-icon-arrow-down {
      color: #c2c2c2;
      padding: 0 10px 0 0;
    }

    ::v-deep .el-icon-arrow-up {
      color: #c2c2c2;
      padding: 0 10px 0 0;
    }

    ::v-deep .el-date-picker.has-sidebar {
      left: 460px !important;
      background-color: #4895db;
    }
  }

  .mb,
  .px {
    ::v-deep .el-form-item__content {
      display: flex;
      width: 280px;
      line-height: 30px;
      border: solid 1px #dcdfe6;
      box-sizing: border-box;
      background-color: #fff;
      border-radius: 5px;

      .el-input__inner {
        border: none;
      }

      span {
        font-size: 14px;
        padding-left: 5px;
        color: #888;
      }
      .el-select{
        width: 100%;
      }
    }
  }

  ::v-deep .el-dialog {
    max-width: 750px;
    min-width: 500px;
    border-radius: 10px;
    background-color: #f9f9f9;
  }

  ::v-deep .el-dialog__header {
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    background-color: #4895db;
    padding: 0;
    border-radius: 10px 10px 0 0;

    span {
      font-size: 16px;
      color: #fff;
    }
  }

  ::v-deep .el-form-item__label {
    line-height: 32px;
  }

  ::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }

  ::v-deep .el-dialog__headerbtn {
    top: 15px;
  }

  ::v-deep .el-dialog__headerbtn .el-dialog__close {
    color: #fff;
    font-size: 18px;
  }

  ::v-deep .el-dialog--center .el-dialog__body {
    text-align: initial;
    display: flex;
    justify-content: center;
  }

  ::v-deep .el-input__icon {
    line-height: 30px;
  }

  ::v-deep .el-form-item__content {
    width: 300px;
    line-height: 30px;
  }

  ::v-deep .el-form-item {
    margin-bottom: 15px;
  }

  ::v-deep .el-dialog__footer {
    padding: 10px 20px 40px;
  }

  ::v-deep .el-button--default {
    width: 130px;
    border-radius: 20px;
    background-color: #f0f0f0;
    color: #aaaaaa;
    border: none;
  }

  ::v-deep .el-button + .el-button {
    width: 130px;
    border-radius: 20px;
    margin-left: 50px;
    background-color: #4895db;
    border-color: #4895db;
  }
}

.courseName,
.profession {
  ::v-deep .el-input__inner {
    width: 200px;
  }

  ::v-deep .el-date-editor.el-input {
    width: 200px;
  }
}

.pictureVersion {
  ::v-deep .el-input__inner {
    width: 140px;
  }

  ::v-deep .el-date-editor.el-input {
    width: 140px;
  }
}
</style>